package com.liu.frame.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import com.liu.frame.R;
import com.liu.frame.customview.TitleBar;
import com.liu.frame.http.IWeiLiCaiHttpService;
import com.liu.frame.http.WeiLiCaiHttpParameters;
import com.liu.frame.http.WeiLiCaiRetrofit;
import com.liu.frame.utils.NetWorkUtil;
import com.liu.frame.utils.OtherUtils;
import com.liu.frame.utils.ToastUtils;

import java.util.HashMap;

import rx.Observable;
import rx.Subscription;


/**
 * Author:Created by 刘智强 on 2015/10/20.
 * Effect:activity的基类
 */
public abstract class BaseActivity extends Activity implements View.OnClickListener {
    protected BaseActivity mActThis = null;
    public boolean isNet;//判断是否有网络
    private String key;//缓存的key
    protected TitleBar myTitleBar;//标题栏
    protected IWeiLiCaiHttpService service; //创建实现接口调用
    protected WeiLiCaiHttpParameters parameters;//接口请求的参数处理
    protected HashMap<String, String> parametersMap;//装私有参数
    protected Subscription subscription;//RXJAVA接口，用来取消订阅


    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActThis = this;
        isNet = NetWorkUtil.isNetwork(this);
        parameters = new WeiLiCaiHttpParameters();
        parametersMap = new HashMap<>();
        service = WeiLiCaiRetrofit.getRetrofit().create(IWeiLiCaiHttpService.class);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentLayout();
        setTitleView();
        initView();
        initEvents();
        /**
         * 无网络去加载缓存
         */
        if (!NetWorkUtil.isNetwork(mActThis)) {
            ToastUtils.myToast(mActThis, "暂无网络");
            loadCache();
        } else {
            objectLogic();
        }

    }


    /**
     * 设置布局
     */
    public abstract void setContentLayout();

    /**
     * 设置title
     */
    public abstract void setTitleView();

    /**
     * 初始化组件
     */
    public abstract void initView();

    /**
     * 设置监听
     */
    public abstract void initEvents();

    /**
     * 业务处理
     */
    public abstract void objectLogic();

    /**
     * 处理监听事件
     */
    public abstract void onClickEvent(View v);

    /**
     * 无网络去加载缓存
     */
    public abstract void loadCache();

    /**
     * 初始化控件
     */
    protected <T extends View> T $(int id) {
        return (T) super.findViewById(id);
    }


    @Override
    public void onClick(View v) {
        if (OtherUtils.fastClick()) {
            onClickEvent(v);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /**
         * 取消网络请求订阅，防止内存泄漏
         */
        if (subscription != null) {
            ToastUtils.myToast(mActThis, "取消订阅消息");
            subscription.unsubscribe();
        }

    }

    //添加fragment
    protected void addFragment(BaseFragment fragment, int fragmentId) {
        if (fragment != null && fragmentId != 0) {
            getFragmentManager().beginTransaction()
                    .replace(fragmentId, fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }


}
